package com.vf.admin.client.controller.ip;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.service.AppIpAuthService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.miaodong.entity.AppIpAuth;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.ip.IpUtils;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/9/25 15:24
 */
@Controller
@RequestMapping(value = "/api/ip")
public class IpPlatformController extends ClientBaseController {

    @Resource
    private AppIpAuthService appIpAuthService;
    @Resource
    private AppUserService appUserService;


    @RequestMapping("/isWhiteIp")
    @ResponseBody
    public Result isWhiteIp(String account, String platform) {

        //自定义名单
        String[] numbers = {"13950104777", "13950104778", "13950104776", "13950104775", "13950104774", "13950104773"};

        String ipAddress = IpUtils.getIpAddress(request);

        //首先判断账号是否已经是会员
        AppUser userAccount = appUserService.getUserAccount(account);
        //不为空
        if (ObjectUtil.isNotEmpty(userAccount)) {

            boolean equals = userAccount.getWindowTicket().equals(1);
            if (equals) {
                return this.sucessJSON(true);
            } else {

                //执行判断ip
                for (String str : numbers) {
                    boolean contains = account.contains(str);
                    if (contains) {
                        //查询库内是否存在数据
                        List<AppIpAuth> accountList = appIpAuthService.getAccountList(account);
                        //判断是否入库
                        if (ObjectUtil.isNotEmpty(accountList) && accountList.size() > 0) {

                            List<String> collect = accountList.stream().map(AppIpAuth::getIp).collect(Collectors.toList());
                            boolean containIp = collect.contains(ipAddress);
                            //判断ip是否存在
                            if (!containIp) {
                                AppIpAuth appIpAuth1 = new AppIpAuth();
                                appIpAuth1.setAccount(account);
                                appIpAuth1.setIp(ipAddress);
                                appIpAuth1.setPlatform(platform);
                                appIpAuth1.setCreatetime(new Date());
                                appIpAuthService.save(appIpAuth1);
                            }
                        }
                        //为空
                        else {
                            AppIpAuth appIpAuth1 = new AppIpAuth();
                            appIpAuth1.setAccount(account);
                            appIpAuth1.setIp(ipAddress);
                            appIpAuth1.setPlatform(platform);
                            appIpAuth1.setCreatetime(new Date());
                            appIpAuthService.save(appIpAuth1);
                        }
                        return this.sucessJSON(true);
                    } else {

                        //判断ip是否跟白名单内的一致
                        List<AppIpAuth> ips = appIpAuthService.getIpList(ipAddress);
                        List<AppIpAuth> accounts = appIpAuthService.getAccountList(account);

                        //只要一个满足条件 返回true
                        if (ObjectUtil.isNotEmpty(ips) || ObjectUtil.isNotEmpty(accounts)) {

                            //如果ip存在 账号不存在保存
                            if (ObjectUtil.isNotEmpty(ips)){


                                //
                                List<AppIpAuth> ipss = appIpAuthService.getIpList(ipAddress);
                                List<String> collect = ipss.stream().map(AppIpAuth::getAccount).collect(Collectors.toList());
                                boolean containAccount = collect.contains(account);
                                if (!containAccount){
                                    //执行保存操作
                                    AppIpAuth appIpAuth1 = new AppIpAuth();
                                    appIpAuth1.setAccount(account);
                                    appIpAuth1.setIp(ipAddress);
                                    appIpAuth1.setPlatform(platform);
                                    appIpAuth1.setCreatetime(new Date());
                                    appIpAuthService.save(appIpAuth1);
                                }


                            }
                            //如果账号存在 ip不存在 保存
                            if (ObjectUtil.isNotEmpty(account)){
                                List<AppIpAuth> accountss = appIpAuthService.getAccountList(account);

                                List<String> collect = accountss.stream().map(AppIpAuth::getIp).collect(Collectors.toList());
                                boolean containsIps = collect.contains(ipAddress);
                                if (!containsIps){
                                    //执行保存操作
                                    AppIpAuth appIpAuth1 = new AppIpAuth();
                                    appIpAuth1.setAccount(account);
                                    appIpAuth1.setIp(ipAddress);
                                    appIpAuth1.setPlatform(platform);
                                    appIpAuth1.setCreatetime(new Date());
                                    appIpAuthService.save(appIpAuth1);
                                }

                            }
                            //如果都存在不执行操作

                            return this.sucessJSON(true);
                        } else {
                            return this.sucessJSON(false);
                        }


                    }
                }
            }
        }
        return this.sucessJSON(false);
    }
}
